﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.ComponentModel;
using System.Web;
using System.Security.Permissions;

namespace Beetle.Web.UI.WebControls
{
    [ToolboxItem(true)]
    public class ContentPresenter : Control
    {
        protected override void Render(HtmlTextWriter writer)
        {
            base.Render(writer);
            string text = string.Empty;
            Control parent = this.Parent;
            Type parentType = null;
            while (parent != null && parent.GetType() != typeof(System.Web.UI.WebControls.FormView))
            {
                parentType = parent.GetType();
                object[] atterbutes = parentType.GetCustomAttributes(typeof(ContentPresenterControlAttribute), true);

                if (atterbutes.Length > 0)
                {
                    text = DataBinder.Eval(parent, ((ContentPresenterControlAttribute)atterbutes[0]).ContentFieldName, this.Format);
                }

                parent = parent.Parent;
            }

            if (!string.IsNullOrEmpty(text))
            {
                writer.Write(text);
            }
        }

        public string Format
        {
            get
            {
                object o = ViewState["Format"];
                return o != null ? (string)o : string.Empty;
            }

            set
            {
                ViewState["Format"] = value;
            }
        }

    }
}
